We want to generate all permutations of a list (e.g. [B, C, D]).
k, decide which element should go there.k+1.k = n (last index), we have a complete permutation.List = [B, C, D], start with k = 0, n = 2
Fix B first → recurse on [C, D]
[B, C, D][B, D, C]Fix C first → recurse on [B, D]
[C, B, D][C, D, B]Fix D first → recurse on [C, B]
[D, C, B][D, B, C]All 6 permutations are produced:
k = current index we’re fillingn = last index